﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;

namespace SPUtility
{

    public enum LogType
    {
        WebPart, ApplicationPage, CommonUtility, UtilityTool, FeatureReceiver, EventHandler, BusinessLayer, DataLayer, Workflow, WebService, UserControl, TimerJob,Informational
    }
   public class LogHandler
     : SPDiagnosticsServiceBase
    {
       
       public static string MaventionDiagnosticAreaName = "ILX Log Diagnostic";
        private static LogHandler _Current;
        public static LogHandler Current
        {
            get
            {
                
                if (_Current == null)
                {
                    _Current = new LogHandler();
                    
                }

                return _Current;
            }
        }

        private LogHandler()
            : base("ILX Logging Service", SPFarm.Local)
        {
           
        }

      


        protected override IEnumerable<SPDiagnosticsArea> ProvideAreas()
        {
           
            List<SPDiagnosticsArea> areas = new List<SPDiagnosticsArea>
        {
            new SPDiagnosticsArea(MaventionDiagnosticAreaName, new List<SPDiagnosticsCategory>
            {
                new SPDiagnosticsCategory("WebPart", TraceSeverity.Unexpected, EventSeverity.Error),
                 new SPDiagnosticsCategory("ApplicationPage", TraceSeverity.Unexpected, EventSeverity.Error),
                  new SPDiagnosticsCategory("UtilityTool", TraceSeverity.Unexpected, EventSeverity.Error),
                   new SPDiagnosticsCategory("FeatureReceiver", TraceSeverity.Unexpected, EventSeverity.Error),
                    new SPDiagnosticsCategory("EventHandler", TraceSeverity.Unexpected, EventSeverity.Error),
                     new SPDiagnosticsCategory("BusinessLayer", TraceSeverity.Unexpected, EventSeverity.Error),
                      new SPDiagnosticsCategory("DataLayer", TraceSeverity.Unexpected, EventSeverity.Error),
                       new SPDiagnosticsCategory("Workflow", TraceSeverity.Unexpected, EventSeverity.Error),
                        new SPDiagnosticsCategory("WebService", TraceSeverity.Unexpected, EventSeverity.Error),
                         new SPDiagnosticsCategory("CommonUtility", TraceSeverity.Unexpected, EventSeverity.Error),
                         new SPDiagnosticsCategory("UserControl", TraceSeverity.Unexpected, EventSeverity.Error),
                         new SPDiagnosticsCategory("TimerJob", TraceSeverity.Unexpected, EventSeverity.Error),
                         new SPDiagnosticsCategory("Informational", TraceSeverity.Monitorable, EventSeverity.Information)
            })
        };

            return areas;
        }

        public static void LogError(LogType logCategory, string errorMessage)
        {
            SPDiagnosticsCategory category = LogHandler.Current.Areas[MaventionDiagnosticAreaName].Categories[logCategory.ToString()];
            LogHandler.Current.WriteTrace(0, category, TraceSeverity.Unexpected, errorMessage);
        }
        public static void LogError(string categoryName, string errorMessage)
        {
            SPDiagnosticsCategory category = LogHandler.Current.Areas[MaventionDiagnosticAreaName].Categories[categoryName];
            LogHandler.Current.WriteTrace(0, category, TraceSeverity.Unexpected, errorMessage);
        }
    }
}
